<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('合同合作伙伴列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>合同编号：</label>
                                <input type="text" name="contractId"/>
                            </li>
                            <li hidden="hidden">
                                <label>合作伙伴id：</label>
                                <input type="text" name="userId"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="contractPartner:contractPartner:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="contractPartner:contractPartner:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="contractPartner:contractPartner:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="contractPartner:contractPartner:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('contractPartner:contractPartner:edit')}]];
        var removeFlag = [[${@permission.hasPermi('contractPartner:contractPartner:remove')}]];

        let contractPartner=[[${contractPartner}]];
        var prefix = ctx + "contractPartner/contractPartner";

        //判断是否是销售合同
        function isSellContract(){
            if(contractPartner.contractType==contractTypeObj.sell){
                return true;
            }else{
                return false;
            }
        }
        var huodan=isSellContract()?'销货单':'提货单';
        var ku=isSellContract()?'出库':'入库';
        $(function() {
            var options = {
                url: prefix + "/list?contractType="+contractPartner.contractType+"&type="+contractPartner.type,
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                detailUrl: ctx + "contract/contract/detail/{id}",
                modalName: "合同合作伙伴",
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'contractId',
                        title: '合同ID',
                        visible: false,
                    },
                {
                    field: 'contractPartnerId',
                    title: '合同ID',
                    visible: false
                },
                {
                    field: 'contractCode',
                    title: '合同编号'
                },
                    {
                        field: 'name',
                        title: "合同名称"
                    },
                    {
                        field: 'totalMoney',
                        title: "合同金额"
                    },
                    // {
                    //     field: 'orderMoney',
                    //     title: isSellContract()?'已出库总金额':'已入库总金额'
                    // },
                    // {
                    //     field: 'useMoney',
                    //     title: isSellContract()?'已出库对账金额':'已入库对账金额'
                    // },
                    // {
                    //     field: 'payTotalMoney',
                    //     title: isSellContract()?'已收款金额（元）':'已付款金额(元)',
                    //     visible: true
                    // },
                    {
                        field: 'startTime',
                        title: '有效开始时间'
                    },
                    {
                        field: 'endTime',
                        title: '有效结束时间'
                    },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-search btn-xs" href="javascript:void(0)" onclick="$.operate.detail(\'' + row.contractId + '\')"><i class="fa fa-search"></i>合同详情</a> ');
                        //actions.push('<a class="btn btn-dark btn-xs "  href="javascript:void(0)" onclick="openContractOrders(' + row.contractId + ','+row.deptId+')"><i class="fa fa-search1"></i>'+huodan+'</a> ');
                        actions.push('<a class="btn btn-dark btn-xs " href="javascript:void(0)" onclick="openContractOrderDetail(\'' + row.contractId + '\')"><i class="fa fa-search1"></i>'+ku+'</a> ');
                        actions.push('<a class="btn btn-dark btn-xs " href="javascript:void(0)" onclick="openContractAccount(\'' + row.contractId + '\')"><i class="fa fa-search1"></i>对账单</a> ');
                        //actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.contractPartnerId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        //actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.contractPartnerId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });


        //订单
        function openContractOrders(contractId,deptId){
            openWin(huodan,ctx+"order/order?contractId="+contractId+"&contractType="+contractPartner.contractType+"&supplierId="+deptId);
        }

        function openContractOrderDetail(id){
            if(isSellContract()){
                if(contractPartner.type==1){//合作伙伴
                    openWindOptions("出库",ctx+"warehouse/orderDetail/sellOrderDetail?contractId="+id+"&roleFlag=1",1200,$(window).height() - 50);
                }else{//租户
                    //出库
                    $.modal.open("出库",ctx+"warehouse/orderDetail/sellOrderDetail?contractId="+id+"&roleFlag=1",1200,$(window).height() - 50,function (index,layero){
                        var wind = layero.find('iframe')[0].contentWindow;
                        var rowsjSON = wind.getAllStoreData();
                        if(rowsjSON.length>0){
                            var contractId=wind.getContractId();
                            var data = {"params[list]":rowsjSON,"contractId":contractId};
                            $.operate.save(ctx+"warehouse/sellStoreDetail/add", data,function (result){
                                if (result.code == web_status.SUCCESS) {
                                    $.modal.msg("入库成功！");
                                    $.modal.close(index);
                                }else{
                                    $.modal.alertError(result.msg);
                                }
                            });
                        }
                    });
                }

            }else{
                if(contractPartner.type==1){//合作伙伴
                    openWindOptions("入库",ctx+"warehouse/orderDetail/mallOrderDetail?contractId="+id+"&roleFlag=1",1200,$(window).height() - 50);
                }else{//租户
                    //入库
                    $.modal.open("入库",ctx+"warehouse/orderDetail/mallOrderDetail?contractId="+id+"&roleFlag=1",1200,$(window).height() - 50,function (index,layero){
                        var wind = layero.find('iframe')[0].contentWindow;
                        var rowsjSON = wind.getAllStoreData();
                        if(rowsjSON.length>0){
                            var contractId=wind.getContractId();
                            var data = {"params[list]":rowsjSON,"contractId":contractId};
                            $.operate.save(ctx+"warehouse/mallStoreDetail/add", data,function (result){
                                if (result.code == web_status.SUCCESS) {
                                    $.modal.msg("入库成功！");
                                    $.modal.close(index);
                                }else{
                                    $.modal.alertError(result.msg);
                                }
                            });
                        }
                    });
                }

            }

        }
        function openContractAccount(id){
            openWin('新增对账单',ctx+"account/account?type="+contractPartner.contractType+"&contractId="+id);
        }

    </script>
</body>
</html>
